export default function (req, res) {
    const { body } = req;
    const { filters = {}, start = 1, limit = 10 } = body;
  
    const totalItems = 155; // Fixed total for consistent pagination mock
    const startIndex = (start - 1) * limit;
  
    // Define data sets for mock generation
    const grades = ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "初一", "初二", "初三", "初四", "高一", "高二", "高三", "大一", "大二", "大三", "大四", "大四以上"];
    const trainers = [{ id: 101, name: "张教练", account: "trainer_zhang" }, { id: 102, name: "李教练", account: "trainer_li" }, { id: 103, name: "王教练", account: "trainer_wang" }];
    const stores = [{ id: 201, name: "线上校区", sn: "SN201" }, { id: 202, name: "线下校区", sn: "SN202" }];
    const deliveryGroups = [{ id: 301, name: "第一小组", fullName: "第一交付小组", code: "DG01" }, { id: 302, name: "第二小组", fullName: "第二交付小组", code: "DG02" }];
    const studyManagers = [{ id: 401, name: "王学管" }, { id: 402, name: "赵学管" }];
    const creators = [{ id: 501, name: "管理员" }];
  
    const generateMockStudent = (index) => {
      const studentId = 60000 + index;
      const grade = grades[Math.floor(Math.random() * grades.length)];
      const isTrainAccount = Math.random() > 0.8;
      const totalClasstime = parseFloat((Math.random() * 200).toFixed(1));
      const usedClasstime = parseFloat((Math.random() * totalClasstime).toFixed(1));
      const refundedClasstime = parseFloat((Math.random() * (totalClasstime - usedClasstime)).toFixed(1));
      const unusedClasstime = parseFloat((totalClasstime - usedClasstime - refundedClasstime).toFixed(1));
      const unscheduledClasstime = parseFloat((unusedClasstime - (Math.random() * unusedClasstime * 0.5)).toFixed(1));
      const hasTrainer = Math.random() > 0.1;
  
      // Use a fixed format for dates to match the API spec
      const formatDate = (date, includeTime = true) => {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        if (!includeTime) {
          return `${year}-${month}-${day}`;
        }
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      };
  
      const createTime = new Date(Date.now() - Math.floor(Math.random() * 365) * 24 * 60 * 60 * 1000);
      const signupDate = formatDate(new Date(createTime), false);
      const updateTime = new Date(createTime.getTime() + Math.floor(Math.random() * 30) * 24 * 60 * 60 * 1000);
      const firstLessonTime = new Date(updateTime.getTime() + Math.floor(Math.random() * 10) * 24 * 60 * 60 * 1000);
      const recentLessonTime = new Date(firstLessonTime.getTime() + Math.floor(Math.random() * 20) * 24 * 60 * 60 * 1000);
      const recentRechargeDate = new Date(updateTime.getTime() + Math.floor(Math.random() * 15) * 24 * 60 * 60 * 1000);
      const acceptTime = new Date(createTime.getTime() + Math.floor(Math.random() * 5) * 24 * 60 * 60 * 1000);
  
      return {
        id: studentId,
        sn: `SN${Math.floor(Math.random() * 900000) + 100000}`,
        name: `学生${index}`,
        grade: grade,
        school: `模拟学校${Math.floor(Math.random() * 5) + 1}`,
        trainAccount: isTrainAccount,
        totalClasstime: totalClasstime,
        unusedClasstime: unusedClasstime,
        unscheduledClasstime: unscheduledClasstime,
        usedClasstime: usedClasstime,
        scheduledUntaughtClasstime: parseFloat((Math.random() * unscheduledClasstime * 0.5).toFixed(1)),
        refundedClasstime: refundedClasstime,
        updateTime: formatDate(updateTime),
        firstSignUpLessonTime: formatDate(firstLessonTime),
        recentSignupLessonTime: formatDate(recentLessonTime),
        disabled: Math.random() > 0.95,
        createTime: formatDate(createTime),
        mainTrainer: hasTrainer ? trainers[Math.floor(Math.random() * trainers.length)] : null,
        substituteTrainer: hasTrainer ? trainers[Math.floor(Math.random() * trainers.length)] : null,
        store: stores[Math.floor(Math.random() * stores.length)],
        deliveryGroup: deliveryGroups[Math.floor(Math.random() * deliveryGroups.length)],
        studyManager: studyManagers[Math.floor(Math.random() * studyManagers.length)],
        signupDate: signupDate,
        recentRechargeDate: formatDate(recentRechargeDate, false),
        creator: creators[0],
        province: '台湾省', // Assuming a fixed location for this mock
        city: '台北市',
        acceptor: {
          keyword: `接单人${Math.floor(Math.random() * 5) + 1}`,
        },
        acceptTime: formatDate(acceptTime),
      };
    };
  
    const mockDataList = Array.from({ length: totalItems }, (_, i) => generateMockStudent(i));
    
    // Implement filtering logic based on the request body
    const filteredList = mockDataList.filter(student => {
      // Keyword filters (student name/SN, school)
      if (filters.student && !(student.name.includes(filters.student) || student.sn.includes(filters.student))) {
        return false;
      }
      if (filters.school && !student.school.includes(filters.school)) {
        return false;
      }
      // Grade filter
      if (filters.grade && student.grade !== filters.grade) {
        return false;
      }
      // Trainer/Store/StudyManager ID filters (not fully implemented, only keyword mock)
      // Here, we just check if the keyword filter is present
      if (filters.mainTrainer && filters.mainTrainer.keyword && !(student.mainTrainer && student.mainTrainer.name.includes(filters.mainTrainer.keyword))) {
        return false;
      }
      if (filters.store && filters.store.keyword && !(student.store && student.store.name.includes(filters.store.keyword))) {
        return false;
      }
      // Classtime range filters
      if (filters.totalClasstimeMin && student.totalClasstime < filters.totalClasstimeMin) {
        return false;
      }
      if (filters.totalClasstimeMax && student.totalClasstime > filters.totalClasstimeMax) {
        return false;
      }
      // Add more filter logic here as needed...
  
      return true;
    });
  
    // Apply pagination to the filtered list
    const paginatedList = filteredList.slice(startIndex, startIndex + limit);
  
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 0,
          message: '成功！',
          data: {
            current: start,
            size: limit,
            total: filteredList.length,
            list: paginatedList,
            summary: {}, // Placeholder for summary
          },
          timestamp: Date.now(),
          requestId: 'requestId_' + Math.random().toString(36).substring(2, 15),
          signature: 'signature_' + Math.random().toString(36).substring(2, 15),
        });
      }, 100);
    });
  }